<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>$(NetCoreAppCurrent)</TargetFramework>
    <AssemblyName>Microsoft.Interop.Tests.NativeExports</AssemblyName>
    <AllowUnsafeBlocks>true</AllowUnsafeBlocks>
    <EnableDynamicLoading>true</EnableDynamicLoading>
    <DnneAddGeneratedBinaryToProject>true</DnneAddGeneratedBinaryToProject>
    <DnneGenRollForward>Major</DnneGenRollForward>
    <!-- NativeExports should use the live built apphost. We need to define a dependency
         to the apphost to make sure that it is built before this project invokes DNNE. -->
    <UseLocalAppHostPack>false</UseLocalAppHostPack>
    <!-- To integrate with DNNE's architecture calculation, we need to set the RID for this project. -->
    <RuntimeIdentifier>$(OutputRID)</RuntimeIdentifier>
    <AppHostRuntimeIdentifier>$(PackageRID)</AppHostRuntimeIdentifier>
    <_TargetsAppleOS Condition="'$(TargetOS)' == 'osx' or '$(TargetOS)' == 'maccatalyst' or
      '$(TargetOS)' == 'ios' or '$(TargetOS)' == 'tvos' or '$(TargetOS)' == 'iossimulator' or
      '$(TargetOS)' == 'tvossimulator'">true</_TargetsAppleOS>
  </PropertyGroup>

  <ItemGroup>
    <Compile Include="..\..\TestAssets\SharedTypes\ComInterfaces\**\*.cs" Link="ComInterfaceGenerator\ComInterfaces\%(RecursiveDir)\%(FileName).cs" />
    <Compile Include="$(CommonPath)DisableRuntimeMarshalling.cs" Link="Common\DisableRuntimeMarshalling.cs" />
  </ItemGroup>

  <!-- Until we use the live app host, use a prebuilt from the SDK.
       Issue: https://github.com/dotnet/runtime/issues/58109. -->
  <ItemGroup Condition="'$(UseLocalAppHostPack)' != 'true' and '$(NetCoreAppToolCurrent)' != '$(NetCoreAppCurrent)'">
    <KnownAppHostPack Include="@(KnownAppHostPack->WithMetadataValue('Identity', 'Microsoft.NETCore.App')->WithMetadataValue('TargetFramework', '$(NetCoreAppToolCurrent)'))"
                      TargetFramework="$(NetCoreAppCurrent)" />
  </ItemGroup>

  <ItemGroup>
    <PackageReference Include="DNNE" Version="$(DNNEVersion)" />
  </ItemGroup>

  <ItemGroup>
    <ProjectReference Include="..\SharedTypes\SharedTypes.csproj" />
    <ProjectReference Include="..\..\Ancillary.Interop\Ancillary.Interop.csproj" />
  </ItemGroup>

  <Target Name="GetUnixBuildArgumentsForDNNE" Condition="'$(OS)' == 'Unix'">
    <PropertyGroup>
      <NativeCompiler>$(Compiler)</NativeCompiler>
      <NativeCompiler Condition="'$(NativeCompiler)' == ''">clang</NativeCompiler>
    </PropertyGroup>

    <Exec Command="sh -c 'build_arch=&quot;$(TargetArchitecture)&quot; compiler=&quot;$(NativeCompiler)&quot; . &quot;$(RepositoryEngineeringDir)/common/native/init-compiler.sh&quot; &amp;&amp; echo $CC' 2>/dev/null"
          EchoOff="true"
          ConsoleToMsBuild="true"
          StandardOutputImportance="Low">
      <Output TaskParameter="ConsoleOutput" PropertyName="DnneCompilerCommand" />
    </Exec>

    <Exec Command="sh -c 'build_arch=&quot;$(TargetArchitecture)&quot; compiler=&quot;$(NativeCompiler)&quot; . &quot;$(RepositoryEngineeringDir)/common/native/init-compiler.sh&quot; &amp;&amp; echo $LDFLAGS' 2>/dev/null"
          EchoOff="true"
          ConsoleToMsBuild="true"
          StandardOutputImportance="Low">
      <Output TaskParameter="ConsoleOutput" PropertyName="DnneLinkerCommand" />
    </Exec>
  </Target>

  <Target Name="GetUnixCrossBuildArgumentsForDNNE"
          Condition="'$(CrossBuild)' == 'true' and
            '$(OS)' == 'Unix' and '$(_TargetsAppleOS)' != 'true'">

    <Exec Command="cmake -P &quot;$(RepositoryEngineeringDir)/native/output-toolchain-info.cmake&quot;"
          EchoOff="true"
          ConsoleToMsBuild="true"
          StandardOutputImportance="Low"
          EnvironmentVariables="TARGET_BUILD_ARCH=$(TargetArchitecture)">
      <Output TaskParameter="ConsoleOutput" PropertyName="CrossTargetXml" />
    </Exec>

    <XmlPeek XmlContent="$(CrossTargetXml)" Query="toolchain-info/target-triple/text()">
      <Output TaskParameter="Result" PropertyName="TargetTriple" />
    </XmlPeek>
    <XmlPeek XmlContent="$(CrossTargetXml)" Query="toolchain-info/linker-args/text()">
      <Output TaskParameter="Result" PropertyName="DnneLinkerUserFlags" />
    </XmlPeek>
    <XmlPeek XmlContent="$(CrossTargetXml)" Query="toolchain-info/compiler-args/text()">
      <Output TaskParameter="Result" PropertyName="DnneCompilerUserFlags" />
    </XmlPeek>

    <PropertyGroup>
      <CommonToolchainArgs>--target=$(TargetTriple) --gcc-toolchain=$(ROOTFS_DIR)/usr --sysroot=$(ROOTFS_DIR)</CommonToolchainArgs>
      <DnneLinkerUserFlags>$(CommonToolchainArgs) $(DnneLinkerCommand) $(DnneLinkerUserFlags.Replace(';',' '))</DnneLinkerUserFlags>
      <DnneCompilerUserFlags>$(CommonToolchainArgs) $(DnneCompilerUserFlags.Replace(';',' '))</DnneCompilerUserFlags>
    </PropertyGroup>
  </Target>

  <Target Name="GetAppleBuildArgumentsForDNNE" Condition="'$(_TargetsAppleOS)' == 'true'">
    <PropertyGroup Condition=" '$(TargetOS)' == 'maccatalyst'">
      <TargetTriple Condition="'$(TargetArchitecture)' == 'arm64'">arm64-apple-ios14.2-macabi</TargetTriple>
      <TargetTriple Condition="'$(TargetArchitecture)' == 'x64'">x86_64-apple-ios13.5-macabi</TargetTriple>
    </PropertyGroup>
    <PropertyGroup Condition="'$(TargetOS)' == 'osx'">
      <TargetTriple Condition="'$(TargetArchitecture)' == 'arm64'">arm64-apple-macos11</TargetTriple>
      <TargetTriple Condition="'$(TargetArchitecture)' == 'x64'">x86_64-apple-macos10.15</TargetTriple>
      <XCodeSdkName>macosx</XCodeSdkName>
    </PropertyGroup>

    <Error Condition="'$(TargetTriple)' == ''" Text="A target triple was not specified for the native components build. Update the 'GetAppleBuildArgumentsForDNNE' target to specify a triple." />
    <Error Condition="'$(XCodeSdkName)' == ''" Text="The name of the XCode SDK for the target platform, as passed to xcrun to locate the sdk, must be specified." />

    <!-- xcrun is used to locate the XCode SDKs and tools within them. See the xcrun manpage for usage information. -->
    <Exec Command="xcrun --sdk $(XCodeSdkName) --show-sdk-path"
          EchoOff="true"
          ConsoleToMsBuild="true"
          StandardOutputImportance="Low">
      <Output TaskParameter="ConsoleOutput" PropertyName="SysRootIncludePath" />
    </Exec>

    <PropertyGroup>
      <DnneLinkerUserFlags>-target $(TargetTriple)</DnneLinkerUserFlags>
      <DnneCompilerUserFlags>-isysroot &quot;$(SysRootIncludePath)&quot; -target $(TargetTriple)</DnneCompilerUserFlags>
    </PropertyGroup>
  </Target>

  <Target Name="GetBuildArgumentsForDNNE"
          DependsOnTargets="ResolveFrameworkReferences;
                            GetUnixBuildArgumentsForDNNE;
                            GetAppleBuildArgumentsForDNNE;
                            GetUnixCrossBuildArgumentsForDNNE"
          BeforeTargets="DnneBuildNativeExports">
    <PropertyGroup>
      <DnneNetHostDir>$([System.IO.Path]::GetDirectoryName('$(AppHostSourcePath)'))</DnneNetHostDir>
    </PropertyGroup>
  </Target>
</Project>
